package com.doufeng.base.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.doufeng.base.dto.LoginDTO;
import com.doufeng.base.entity.User;
import com.doufeng.base.service.UserService;
import com.doufeng.common.result.Result;
import com.doufeng.common.util.BCryptUtil;
import com.doufeng.common.util.JwtUtil;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/base/login")
public class LoginController {

    @Resource
    private UserService userService;
    @PostMapping("/pwd")
    public Result<String> loginByPwd(@RequestBody LoginDTO dto) {
        User user = userService.getOne(new LambdaQueryWrapper<User>()
                .eq(User::getUsername, dto.getUsername())
                .eq(User::getDeleted, 0));
        if (user == null || !BCryptUtil.matches(dto.getPassword(), user.getPassword())) {
            return Result.fail(401, "账号或密码错误");
        }
        // TODO 校验账号密码
        String token = JwtUtil.createToken(1001L, "USER");
        return Result.success(token);
    }
}